﻿using System;
using System.Collections.Generic;
using System.Data.Spatial;
using System.Linq;
using System.Web;

namespace MovingChame.Web.Helpers
{
    public static class DbGeographyHelper
    {
        public static DbGeography GeographyAddOperator(DbGeography point, double length)
        {
            if ((!point.Longitude.HasValue) || (!point.Latitude.HasValue))
                throw new Exception("Invalid operand. Null point");

            DbGeography result;
            double resultLong, resultLat;

            resultLong = (double)point.Longitude + length;
            resultLat = (double)point.Latitude + length;
            result = DbGeography.FromText("POINT(" + resultLong.ToString() + " " + resultLat.ToString() + ")");

            return result;
        }

        public static bool GeographyInArea(DbGeography center, double radius, DbGeography point)
        {
            double distance;
            if (center.Distance(point) != null)
                distance = (double)center.Distance(point);
            else
                throw new Exception("Cannot calculate distance between center and point");
            if (distance <= radius)
                return true;
            else
                return false;
        }
    }
}